package com.hjyang.docx.convert.pdf.doc.conver;

import com.gsafety.fileauxiliarysystem.utils.doc.config.ConfigBean;
import com.lowagie.text.Font;
import com.lowagie.text.pdf.BaseFont;
import fr.opensagres.xdocreport.itext.extension.font.ITextFontRegistry;
import fr.opensagres.xdocreport.utils.StringUtils;
import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.*;

import java.awt.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DocxToPdf {
    public static void main(String[] args) {
        word2pdf("D:/word2pdfFile/123.doc", "D:/word2pdfFile/123x.pdf");
    }
    
    public static String word2pdf(String wordFilePath, String pdfFilePath) {
        try {
            InputStream source = new FileInputStream(wordFilePath);
            OutputStream target = new FileOutputStream(pdfFilePath);
            Map<String, String> params = new HashMap<String, String>();
            PdfOptions options = PdfOptions.create();
            wordConverterToPdf(source, target, options, params);
        } catch (Exception e) {
            e.printStackTrace();
            return "docx";
        }
        return "pdf";
    }
    
    public static void wordConverterToPdf(InputStream source, OutputStream target, Map<String, String> params) throws Exception {
        wordConverterToPdf(source, target, null, params);
    }
    
    public static void wordConverterToPdf(InputStream source, OutputStream target, PdfOptions options, Map<String, String> params) throws Exception {
        XWPFDocument doc = new XWPFDocument(source);
        paragraphReplace(doc.getParagraphs(), params);
        for (XWPFTable table : doc.getTables()) {
            for (XWPFTableRow row : table.getRows()) {
                for (XWPFTableCell cell : row.getTableCells()) {
                    paragraphReplace(cell.getParagraphs(), params);
                }
            }
        }
        options.fontProvider(new ITextFontRegistry() {
            public Font getFont(String familyName, String encoding, float size, int style, Color color) {
                try {
                    //BaseFont bfChinese = BaseFont.createFont("D:/word2pdfFile/simsun.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                    BaseFont bfChinese = BaseFont.createFont(ConfigBean.getFontPath(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                    Font fontChinese = new Font(bfChinese, size, style, color);
                    if (familyName != null)
                        fontChinese.setFamily(familyName);
                    return fontChinese;
                } catch (Throwable e) {
                    e.printStackTrace();
                    return ITextFontRegistry.getRegistry().getFont(familyName, encoding, size, style, color);
                }
            }
        });
        PdfConverter.getInstance().convert(doc, target, options);
    }
    
    private static void paragraphReplace(List<XWPFParagraph> paragraphs, Map<String, String> params) {
        if (params != null) {
            for (XWPFParagraph p : paragraphs) {
                for (XWPFRun r : p.getRuns()) {
                    String content = r.getText(r.getTextPosition());
                    if (StringUtils.isNotEmpty(content) && params.containsKey(content)) {
                        r.setText(params.get(content), 0);
                    }
                }
            }
        }
    }
}